/***************************************************
 ** Create by Hasaki on 2021/11/27.
 ** This file is part of the CSTL project.
 ** This file follows version 3 of GNU License.
 ** About GNU, see <https://www.gnu.org/licenses/>.
 **
 ** 本文件由Hasaki(间宫羽咲sama)于2021年11月27日创建。
 ** 本文件是CSTL(C-STL)项目的一部分。
 ** 本文件遵从GNU-v3协议。
 ** 关于GNU协议，见<https://www.gnu.org/licenses/>。
 ***************************************************/
#include <stdio.h>
#include "HskUtil.h"
#include "HskBuffer.h"

int main() {

  printf("\n--- Testing: Function BufPushSlice in HskBuffer.h ---\n\n");
  char *hello = "Hello ";
  char *world = "world!";
  Buffer* buf1 = BufCreate(7);
  BufDispMsg(buf1);                                   // expected: this.unreadSize = 0, this.size = 0, this.capa = 7.
  BufPushSlice(buf1, hello, hello+6);
  BufDispMsg(buf1);                                   // expected: this.unreadSize = 6, this.size = 6, this.capa = 7.
  BufDispAllString(buf1);                             // expected: this.allString = Hello
  Buffer* buf2 = BufCreate(7);
  BufPushSlice(buf2, world, world+6);
  BufDispMsg(buf2);                                   // expected: this.unreadSize = 6, this.size = 6, this.capa = 7.
  BufDispAllString(buf2);                             // expected: this.allString = world!
  BufPushSlice(buf1, buf2->_read, buf2->_end);
  BufDispMsg(buf1);                                   // expected: this.unreadSize = 12, this.size = 12, this.capa = 16.
  BufDispAllChar(buf1, buf1->_read, buf1->_end);      // expected: Hello world!
  BufDispAllInt(buf1, buf1->_read, buf1->_last);      // expected: 72 101 108 108 111 32 119 111 114 108 100 33 0 0 0 0

  printf("\n--- Testing: Function BufReadBuf in HskBuffer.h ---\n\n");

  BufDelN(buf1, 6);
  BufDispMsg(buf1);                                   // expected: this.unreadSize = 6, this.size = 6, this.capa = 16.
  BufDispAllChar(buf1, buf1->_read, buf1->_end);      // expected: Hello 
  BufReadBuf(buf1, buf2, 5);
  BufDispMsg(buf1);                                   // expected: this.unreadSize = 11, this.size = 11, this.capa = 16.
  BufDispAllChar(buf1, buf1->_read, buf1->_end);      // expected: Hello world
  BufDispMsg(buf2);                                   // expected: this.unreadSize = 1, this.size = 6, this.capa = 7.
  BufDispAllChar(buf2, buf2->_read, buf2->_end);      // expected: !
  BufDispAllChar(buf2, buf2->_begin, buf2->_end);     // expected: world!
  BufDispAllInt(buf2, buf2->_begin, buf2->_last);     // expected: 119 111 114 108 100 33 0
  BufPush(buf2, '?');
  BufDispMsg(buf2);                                   // expected: this.unreadSize = 2, this.size = 7, this.capa = 7.
  BufDispAllChar(buf2, buf2->_read, buf2->_end);      // expected: !?
  BufDispAllChar(buf2, buf2->_begin, buf2->_end);     // expected: world!?
  BufDispAllInt(buf2, buf2->_begin, buf2->_last);     // expected: 119 111 114 108 100 33 63
  printf("buf2[3] = %c\n", *BufGet(buf2, 2));         // expected: buf2[3] = r
  printf("buf2[read + 1] = %c\n", *BufUnreadGet(buf2, 1));
                                                      // expected: buf2[read + 1] = ?
  BufPush(buf2, '+');
  BufDispMsg(buf2);                                   // expected: this.unreadSize = 3, this.size = 3, this.capa = 7.
  BufDispAllChar(buf2, buf2->_begin, buf2->_end);     // expected: !?+
  BufDispAllInt(buf2, buf2->_begin, buf2->_last);     // expected: 33 63 43 0 0 0 0

  getchar();
  return 0;
}
